<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SiteMenus.php 320 2008-11-23 07:18:37Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalSiteMenuRecord');
Prado::using('Application.Common.Data.PortalSiteMenuItemRecord');

Prado::using('Application.Common.Data.PortalRoleRecord');

class SiteMenus extends PortalAdminPage
{
	public function dataBind()
	{
		$this->dlSiteMenuList->DataSource	= PortalSiteMenuRecord::finder()->findAll();
		$this->dlSiteMenuList->dataBind();

		$this->Role->DataSource	= PortalRoleRecord::finder()->findAll();
		$this->Role->dataBind();
	}

	public function OnInit($param)
	{
		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('Manage sitemenus');
	}

	public function AddMenu($sender, $param)
	{
		if(!$this->IsValid)
		{
			return;
		}

		$sm = new PortalSiteMenuRecord();
		$sm->name	= $this->NewMenuName->Text;
		$sm->role	= TPropertyValue::ensureInteger($this->Role->SelectedValue);
		$sm->active	= $this->Active->checked;
		$sm->save();
		$this->gotoPage("Admin.SiteMenus");
	}

	public function ValidateMenuName($sender, $param)
	{
		$sm = PortalSiteMenuRecord::finder()->findByname($param->Value);
		if($sm)
		{
			$param->IsValid = false;
		}
		else
		{
			$param->IsValid = true;
		}
	}

	public function itemCommand($s,$p)
	{
		switch($p->CommandName)
		{
		}
	}

	public function deleteItem($sender, $param)
	{
		$id = $this->dlSiteMenuList->DataKeys[$param->Item->ItemIndex];
		PortalSiteMenuRecord::finder()->deleteByid($id);
		$this->dataBind();
	}

	public function editItem($sender,$param)
	{
		$this->dlSiteMenuList->SelectedItemIndex	= -1;
		$this->dlSiteMenuList->EditItemIndex		= $param->Item->ItemIndex;
		$this->dataBind();
	}

	public function cancelItem($sender,$param)
	{
		$this->dlSiteMenuList->SelectedItemIndex	= -1;
		$this->dlSiteMenuList->EditItemIndex		= -1;
		$this->dataBind();
	}

	public function updateItem($sender,$param)
	{
		$item		= $param->Item;
		$id			= $this->dlSiteMenuList->DataKeys[$item->ItemIndex];
		$active		= $item->edtActive->getChecked();
		$sm			= PortalSiteMenuRecord::finder()->findByid($id);
		$sm->active = $active;
		$sm->role	= TPropertyValue::ensureInteger($item->edtRole->SelectedValue);
		$sm->save();

		$this->dlSiteMenuList->EditItemIndex=-1;
		$this->dataBind();
	}

	public function Role2Txt($id)
	{
		switch($id)
		{
			case 0: return Prado::localize("All");
			default:
				$r = PortalRoleRecord::finder()->findByid($id);
				return $r->name;
		}
	}

	public function getRoles()
	{
		return PortalRoleRecord::finder()->findAll();
	}

	public function ValidateRoleValue($role)
	{
		if($role !== '0' && $reole !== 0 && $role !== '')
			return $role;
	}

	public function HaveMenus($id)
	{
		return $this->DataAccess->createCommand("select count(id) from tblsitemenuitem where smid=$id")->queryScalar() == 0 ? Prado::localize('No') : Prado::localize('Yes');
	}
}
?>